package mya.mes.mdc.logic.mqtt;

import mya.common.util.Utils;
import mya.mes.mdc.local.alarm.MdcAlarmResult;
import mya.mes.mdc.logic.alarm.filter.AlarmFilter;
import mya.mes.mdc.logic.alarm.mqtt.MqttAlarmProcessor;
import mya.mes.mdc.model.alarm.MdcAlarm;
import mya.mes.mdc.mongodb.DbCollections;
import mya.mes.mdc.mqtt.MqttClientManager;
import mya.mes.mdc.share.mqttread.MqttMsgProcessor;
import mya.mes.mdc.share.plc.PlcTagVoFilter;
import mya.mes.mdc.share.plc.PlcTagVoProcessor;
import mya.mes.mdc.share.vo.MdcMqttDefaultTopic;
import mya.mes.mdc.share.vo.mqtt.MqttMsgReceived;
import mya.mes.mdc.share.vo.plc.PlcTagVo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MdcMqttDefaultProcessor implements MqttMsgProcessor {

    private final Long zoneId = 1L;

    private final DataSaveToMongoProcessor dataProcessor;

    private final WebsocketBroadCast websocketBroadCast;

    private final List<PlcTagVoProcessor> plcTagVoProcessorList = new ArrayList<>();

    private PlcTagVoFilter alarmTagVoFilter;
    private final MqttAlarmProcessor alarmProcessor;

    public MdcMqttDefaultProcessor() {

        alarmProcessor = new MqttAlarmProcessor(zoneId);
        dataProcessor = new DataSaveToMongoProcessor();
        websocketBroadCast = new WebsocketBroadCast();
        alarmTagVoFilter = new AlarmFilter();
    }

    @Override
    public Boolean accept(Long zoneId) {
        return this.zoneId.equals(zoneId);
    }

    @Override
    public void mqttReceive(MqttMsgReceived mqttMsg) throws Exception {
        final Long mqttMsgId = mqttMsg.mqttMsgId;
        List<PlcTagVo> voList = null;
        MdcAlarmResult alarmResult = null;
        try {
            voList = MqttClientManager.getInstance().getDataConverter()
                    .processMsg(mqttMsg.mqttTopic, mqttMsg.mqttContent);
            if (MdcMqttDefaultTopic.MDC_REPORT_IN.equals(mqttMsg.mqttTopic)) {
                dataProcessor.process(mqttMsgId, voList, DbCollections.MDC_REPORT_DATA);
            } else {
                dataProcessor.process(mqttMsgId, voList);
            }

            alarmResult = processAlarm(mqttMsgId, voList);
            //取消3d的数据推送
//            for (PlcTagVoProcessor processor : plcTagVoProcessorList) {
//                processor.process(mqttMsgId, voList);
//            }cable_picture_code
        } catch (Throwable t) {
            t.printStackTrace();
            return;
        }
//
//        alarmResult = new MdcAlarmResult();
//        List<MdcAlarm> alarmCreatedList = new ArrayList<MdcAlarm>();
//        List<MdcAlarm> alarmCloseList = new ArrayList<MdcAlarm>();
//        MdcAlarm create = new MdcAlarm();
//        create.setObjectId(10000002L);
//        create.setZhCN("z1报警");
//        create.setArea("LINE5");
//        create.setAlarmDate("2020-07-19");
//        create.setAlarmTime("23:21:10");
//
//        MdcAlarm create_ = new MdcAlarm();
//        create_.setObjectId(10000003L);
//        create_.setZhCN("z2报警");
//        create_.setArea("LINE5");
//        create_.setAlarmDate("2020-07-19");
//        create_.setAlarmTime("23:21:10");
//
//        alarmCreatedList.add(create);
//        alarmCreatedList.add(create_);
//
//        MdcAlarm close = new MdcAlarm();
//        close.setObjectId(10000003L);
//        close.setZhCN("z3报警");
//        close.setArea("LINE5");
//        close.setAlarmDate("2020-07-19");
//        close.setAlarmTime("23:21:10");
//        close.setCloseDate("2020-07-19");
//        close.setCloseTime("23:21:15");
//
//        alarmCloseList.add(close);
//
//        alarmResult.alarmCloseList = alarmCloseList;
//        alarmResult.alarmCreatedList = alarmCreatedList;
        websocketBroadCast.broadcast(mqttMsgId, voList, alarmResult);
    }

    private MdcAlarmResult processAlarm(Long mqttMsgId, List<PlcTagVo> voList) throws Exception {
        List<PlcTagVo> alarmVoList = alarmTagVoFilter.filter(voList);
        if (Utils.isNullOrEmpty(alarmVoList)) {
            return null;
        }
        return alarmProcessor.process(mqttMsgId, alarmVoList);
    }

    @Override
    public synchronized void addPlcTagVoProcessor(PlcTagVoProcessor processor) {
        if (processor == null || processor.getId() == null || processor.getId().isEmpty()) {
            return;
        }
        removePlcTagVoProcessor(processor);
        plcTagVoProcessorList.add(processor);
    }

    @Override
    public synchronized void removePlcTagVoProcessor(PlcTagVoProcessor processor) {
        if (processor == null || processor.getId() == null || processor.getId().isEmpty()) {
            return;
        }

        Iterator<PlcTagVoProcessor> it = plcTagVoProcessorList.iterator();

        while (it.hasNext()) {
            PlcTagVoProcessor tmp = it.next();

            if (processor.getId().equals(tmp.getId())) {
                it.remove();

                break;
            }
        }
    }

    public static void main(String[] args) throws Exception {
        List<PlcTagVo> voList = new ArrayList<PlcTagVo>();
        PlcTagVo plcTagVo = new PlcTagVo();
        plcTagVo.tagName = "AI1001";
        plcTagVo.tagIntValue = 1000;

        PlcTagVo plcTagVo_ = new PlcTagVo();
        plcTagVo_.tagBoolValue = true;
        plcTagVo_.tagName = "DI1002";
        voList.add(plcTagVo);
        voList.add(plcTagVo_);
        WebsocketBroadCast websocketBroadCast = new WebsocketBroadCast();

        MdcAlarmResult alarmResult = new MdcAlarmResult();

        List<MdcAlarm> alarmCreatedList = new ArrayList<MdcAlarm>();
        List<MdcAlarm> alarmCloseList = new ArrayList<MdcAlarm>();
        MdcAlarm create = new MdcAlarm();
        create.setObjectId(10000002L);
        create.setZhCN("z1报警");
        create.setArea("LINE5");
        create.setAlarmDate("2020-07-19");
        create.setAlarmTime("23:21:10");

        MdcAlarm create_ = new MdcAlarm();
        create_.setObjectId(10000003L);
        create_.setZhCN("z2报警");
        create_.setArea("LINE5");
        create_.setAlarmDate("2020-07-19");
        create_.setAlarmTime("23:21:10");

        alarmCreatedList.add(create);
        alarmCreatedList.add(create_);

        MdcAlarm close = new MdcAlarm();
        close.setObjectId(10000003L);
        close.setZhCN("z3报警");
        close.setArea("LINE5");
        close.setAlarmDate("2020-07-19");
        close.setAlarmTime("23:21:10");
        close.setCloseDate("2020-07-19");
        close.setCloseTime("23:21:15");

        alarmCloseList.add(close);

        websocketBroadCast.broadcast(123456789L, voList, alarmResult);
    }

}
